﻿using ContractWebApi.BtbContractEnum;
using ContractWebApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using ContractWebApi.Models.EventModel;

namespace ContractWebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OpenContractApiController : ControllerBase
    {
        private readonly ILogger<OpenContractApiController> _logger;

        public OpenContractApiController(ILogger<OpenContractApiController> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 电子合同回调通知
        /// </summary>
        /// <param name="originData">源数据</param>
        /// <returns></returns>
        [HttpPost]
        public BtbResponseCertDto BtbContractNoticeForEvent([FromBody] BtbContractNoticeDto originData)
        {
            try
            {
                var result = new BtbResponseCertDto
                {
                    Code = "0",
                    Msg = ""
                };

                _logger.LogInformation($"\n******接收到回调******\n{JsonConvert.SerializeObject(originData)}\n*****开始尝试序列化******");

                switch (originData.EventType)
                {
                    case NoticeEventTypeEnum.AuthenticationResultEvent:

                        _logger.LogInformation($"\n******此回调为认证结果回调******\n{originData.Data}");
                        var test = originData.Data.ToString();
                        if (test != null)
                        {
                            var input = JsonConvert.DeserializeObject<AuthenticationResultInput>(test);
                            if (input == null)
                            {
                                result.Code = "400";
                                result.Msg = "认证信息未序列化成功";
                                return result;
                            }
                            originData.Data = input;

                            var logModel = JsonConvert.SerializeObject(originData);
                            _logger.LogInformation($"\n****** 认证结果序列化成功 ******\n{JsonConvert.SerializeObject(originData)}");
                        }

                        break;
                    case NoticeEventTypeEnum.HumanAuditEvent:
                        break;
                    case NoticeEventTypeEnum.SignatureAudit:
                        break;
                    case NoticeEventTypeEnum.StampCreationAudit:
                        _logger.LogInformation($"\n****** 此回调为印章创建申请审核事件 ******\n{originData.Data}");
                        var stampCreation = originData.Data.ToString();
                        if (stampCreation != null)
                        {
                            var input = JsonConvert.DeserializeObject<StampCreationAuditInput>(stampCreation);
                            if (input == null)
                            {
                                result.Code = "400";
                                result.Msg = "认证信息未序列化成功";
                                return result;
                            }
                            originData.Data = input;

                            var logModel = JsonConvert.SerializeObject(originData);
                            _logger.LogInformation($"\n******印章创建申请结果序列化成功******\n{JsonConvert.SerializeObject(originData)}");

                            _logger.LogInformation($"\n******印章创建申请结果:{input.Event}******\n");
                        }
                        break;
                    case NoticeEventTypeEnum.MarkingMoneyToPublic:
                        break;
                    case NoticeEventTypeEnum.LegalPersonSigningEvent:
                        break;
                    case NoticeEventTypeEnum.UserPayToPlatFormEvent:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
                return result;
            }
            catch (System.Exception ex)
            {
                _logger.LogError($"处理回调失败{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 电子合同回调通知
        /// </summary>
        /// <param name="originData">源数据</param>
        /// <returns></returns>
        [HttpPost]
        public BtbResponseCertDto BtbContractNoticeForSign([FromBody] BtbContractNoticeSignDto originData)
        {
            try
            {
                var result = new BtbResponseCertDto
                {
                    Code = "0",
                    Msg = ""
                };

                _logger.LogInformation($"******接收到回调******\n{JsonConvert.SerializeObject(originData)}\n*****End接收到回调End******");
                if (originData.ResultCode.Equals(SignResultEnum.SignSuccess))
                {
                    _logger.LogInformation($"******签章成功******\n合同Id：{originData.ContractCode}\n*****End签章成功End******");
                }
                else
                {
                    _logger.LogInformation($"******签章失败！******\n签章成功，合同Id：{originData.ContractCode}\n*****签章失败！******");
                }
                return result;
            }
            catch (System.Exception ex)
            {
                _logger.LogError($"处理回调失败{ex.StackTrace}");
                throw;
            }
        }

    }
}
